<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Connecting</title>
<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="../../index.html" title="Chapter 1. Boost.Beast">
<link rel="up" href="../using_websocket.html" title="WebSocket">
<link rel="prev" href="../using_websocket.html" title="WebSocket">
<link rel="next" href="handshaking.html" title="Handshaking">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
<td align="center"><a href="../../../../../../index.html">Home</a></td>
<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../using_websocket.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../using_websocket.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="handshaking.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="beast.using_websocket.establishing_connections"></a><a class="link" href="establishing_connections.html" title="Connecting">Connecting</a>
</h3></div></div></div>
<p>
        Before messages can be exchanged, a websocket stream first needs to be connected,
        and then to have the websocket handshake performed. The stream delegates
        the task of establishing the connection to the next layers. For example,
        if the next layer is a connectible stream or socket object, it can be accessed
        to call the necessary function for connecting. Here we make an outbound connection
        as a client would do.
      </p>
<pre class="programlisting"><span class="identifier">stream</span><span class="special">&lt;</span><span class="identifier">tcp_stream</span><span class="special">&gt;</span> <span class="identifier">ws</span><span class="special">(</span><span class="identifier">ioc</span><span class="special">);</span>
<span class="identifier">net</span><span class="special">::</span><span class="identifier">ip</span><span class="special">::</span><span class="identifier">tcp</span><span class="special">::</span><span class="identifier">resolver</span> <span class="identifier">resolver</span><span class="special">(</span><span class="identifier">ioc</span><span class="special">);</span>

<span class="comment">// Connect the socket to the IP address returned from performing a name lookup</span>
<span class="identifier">get_lowest_layer</span><span class="special">(</span><span class="identifier">ws</span><span class="special">).</span><span class="identifier">connect</span><span class="special">(</span><span class="identifier">resolver</span><span class="special">.</span><span class="identifier">resolve</span><span class="special">(</span><span class="string">"example.com"</span><span class="special">,</span> <span class="string">"ws"</span><span class="special">));</span>
</pre>
<p>
        To accept incoming connections, an acceptor is used. The websocket stream
        may be constructed from the socket returned by the acceptor when an incoming
        connection is established.
      </p>
<pre class="programlisting"><span class="identifier">net</span><span class="special">::</span><span class="identifier">ip</span><span class="special">::</span><span class="identifier">tcp</span><span class="special">::</span><span class="identifier">acceptor</span> <span class="identifier">acceptor</span><span class="special">(</span><span class="identifier">ioc</span><span class="special">);</span>
<span class="identifier">acceptor</span><span class="special">.</span><span class="identifier">bind</span><span class="special">(</span><span class="identifier">net</span><span class="special">::</span><span class="identifier">ip</span><span class="special">::</span><span class="identifier">tcp</span><span class="special">::</span><span class="identifier">endpoint</span><span class="special">(</span><span class="identifier">net</span><span class="special">::</span><span class="identifier">ip</span><span class="special">::</span><span class="identifier">tcp</span><span class="special">::</span><span class="identifier">v4</span><span class="special">(),</span> <span class="number">0</span><span class="special">));</span>
<span class="identifier">acceptor</span><span class="special">.</span><span class="identifier">listen</span><span class="special">();</span>

<span class="comment">// The socket returned by accept() will be forwarded to the tcp_stream,</span>
<span class="comment">// which uses it to perform a move-construction from the net::ip::tcp::socket.</span>

<span class="identifier">stream</span><span class="special">&lt;</span><span class="identifier">tcp_stream</span><span class="special">&gt;</span> <span class="identifier">ws</span><span class="special">(</span><span class="identifier">acceptor</span><span class="special">.</span><span class="identifier">accept</span><span class="special">());</span>
</pre>
<p>
        Alternatively, the incoming connection may be accepted directly into the
        socket owned by the websocket stream, using this overload of the acceptor
        member function.
      </p>
<pre class="programlisting"><span class="comment">// The stream will use the strand for invoking all completion handlers</span>
<span class="identifier">stream</span><span class="special">&lt;</span><span class="identifier">tcp_stream</span><span class="special">&gt;</span> <span class="identifier">ws</span><span class="special">(</span><span class="identifier">net</span><span class="special">::</span><span class="identifier">make_strand</span><span class="special">(</span><span class="identifier">ioc</span><span class="special">));</span>

<span class="comment">// This overload of accept uses the socket provided for the new connection.</span>
<span class="comment">// The function `tcp_stream::socket` provides access to the low-level socket</span>
<span class="comment">// object contained in the tcp_stream.</span>

<span class="identifier">acceptor</span><span class="special">.</span><span class="identifier">accept</span><span class="special">(</span><span class="identifier">get_lowest_layer</span><span class="special">(</span><span class="identifier">ws</span><span class="special">).</span><span class="identifier">socket</span><span class="special">());</span>
</pre>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright © 2016-2019 Vinnie
      Falco<p>
        Distributed under the Boost Software License, Version 1.0. (See accompanying
        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
      </p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../using_websocket.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../using_websocket.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="handshaking.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
